package com.example.cv.controller;

import com.example.cv.model.Admin;
import com.example.cv.model.Candidate;
import com.example.cv.model.Employer;
import com.example.cv.service.AdminService;
import com.example.cv.service.CandidateService;
import com.example.cv.service.EmployerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 周杰
 */
@Controller
public class AdminController {
    public AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    public CandidateService candidateService;

    @Autowired
    public void setCandidateService(CandidateService candidateService) {
        this.candidateService = candidateService;
    }

    public EmployerService employerService;

    @Autowired
    public void setEmployerService(EmployerService employerService) {
        this.employerService = employerService;
    }

    /**
     * description:总登录页面的get形式
     * @param
     * @return java.lang.String
     */
    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    /**
     * description:管理员登陆的post形式
     * @param
     * @return java.lang.String
     */
    @PostMapping("/adminLogin")
    public String adminLogin(Admin admin, HttpSession session, Model model) {
        Admin r = adminService.findAdmin(admin);
        if (r != null) {
            session.setAttribute("admin", r);
            return "redirect:candidateManage";
        } else {
            model.addAttribute("msg","用户名或者密码错误");
            return "login";
        }
    }
    /**
     * description:管理员退出
     * @param
     * @return java.lang.String
     */
    @GetMapping("/adminExit")
    public String exitAdmin(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:login";
    }

    @GetMapping("/candidateManage")
    public String candidateManage(Model model, @RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "9") int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Candidate> candidates=employerService.findAllCandidates();
        //放入分页对象中,第二个参数表示最多连续显示的页数5
        PageInfo<Candidate> pageInfo=new PageInfo<Candidate>(candidates,5);
        model.addAttribute("candidates",candidates);
        model.addAttribute("pageInfo",pageInfo);
        return "candidateManage";
    }

    @GetMapping("/employerManage")
    public String employerManage(Model model, @RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "9") int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Employer> employers=adminService.findAllEmployers();
        //放入分页对象中,第二个参数表示最多连续显示的页数5
        PageInfo<Employer> pageInfo=new PageInfo<Employer>(employers,5);
        model.addAttribute("employers",employers);
        model.addAttribute("pageInfo",pageInfo);
        return "employerManage";
    }

    @GetMapping("/candidateDelete")
    public String candidateDelete(int c_id){
        boolean flag=adminService.deleteCandidateByCId(c_id);
        return "redirect:candidateManage";
    }

    @GetMapping("/employerDelete")
    public String employerDelete(int e_id){
        boolean flag=adminService.deleteEmployerByEId(e_id);
        return "redirect:employerManage";
    }

    @PostMapping("/adminSearchCanByParams")
        public String adminSearchCanByParams(Candidate candidate,Model model){
        List<Candidate>candidates=employerService.findCandidatesByParams(candidate);
        model.addAttribute("candidates",candidates);
        return "candidateManage";
    }

    @PostMapping("/adminSearchEmpByEName")
    public String adminSearchEmpByParams(String e_name,Model model){
        List<Employer>employers=adminService.findEmployersByEName(e_name);
        model.addAttribute("employers",employers);
        return "employerManage";
    }

    @GetMapping("/adminUpdateCandidate")
    public String adminUpdateCandidate(int c_id,Model model){
        Candidate candidate=candidateService.findCandidateById(c_id);
        model.addAttribute("candidate",candidate);
        return "adminUpdateCandidate";
    }

    @PostMapping("/adminUpdateCandidate")
    public String adminUpdateCan(Candidate candidate,Model model){
        boolean flag=candidateService.updateCandidate(candidate);
        if(flag){
            model.addAttribute("modifyMessage","修改成功！");
        }else{
            model.addAttribute("modifyMessage","修改失败！");
        }
        return "adminUpdateCandidate";
    }

    @GetMapping("/adminUpdateEmployer")
    public String adminUpdateEmployer(int e_id,Model model){
        Employer employer=employerService.findEmployerById(e_id);
        model.addAttribute("employer",employer);
        return "adminUpdateEmployer";
    }

    @PostMapping("/adminUpdateEmployer")
    public String adminUpdateEmployer(Employer employer,Model model){
        boolean flag=employerService.updateEmployer(employer);
        if(flag){
            model.addAttribute("modifyMessage","修改成功！");
        }else{
            model.addAttribute("modifyMessage","修改失败！");
        }
        return "adminUpdateEmployer";
    }

    @GetMapping("/adminUpdate")
    public String adminUpdate(int a_id,Model model){
        Admin admin=adminService.findAdminById(a_id);
        model.addAttribute("admin",admin);
        return "adminUpdate";
    }

    @PostMapping("/adminUpdate")
    public String adminUpdate(Admin admin,Model model){
        boolean flag=adminService.updateAdmin(admin);
        if(flag){
            model.addAttribute("modifyMessage","修改成功！");
        }else{
            model.addAttribute("modifyMessage","修改失败！");
        }
        return "adminUpdate";
    }
}
